﻿@model PagedList.IPagedList<TireStore.Models.Product>
@{
    ViewBag.Title = "Product";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
@using PagedList
@using PagedList.Mvc
@using TireStore.Helpers


<div id="Breadcrumb" class="Block Breadcrumb ui-widget-content ui-corner-top ui-corner-bottom">
    <ul>
        <li class='SecondLast'><a href='/admin'><i class="icon-home" style="font-size: 14px;"></i>Trang chủ</a></li>
        <li class='Last'><span>Danh sách sản phẩm</span></li>
    </ul>
</div>
<div style="clear: both;"></div>


<div id="cph_Main_ContentPane">




    <div class="widget">
        <div class="widget-title">
            <h4><i class="icon-list-alt"></i>&nbsp;Danh sách sản phẩm</h4>

            <div class="ui-widget-content ui-corner-top ui-corner-bottom">
                <div id="toolbox">

                    <div style="float: right;" class="toolbox-content">

                        <table class="toolbar">
                            <tr>

                                <td align="center">
                                    <a onclick="javascript:return PopupHelp(&#39;http://www.bizweb.vn/PopupHelp.aspx?faqcat=25&#39;);" id="cph_Main_ctl00_toolbox_rptAction_lbtAction_0" title="Trợ giúp" class="toolbar btn btn-info" href="javascript:__doPostBack(&#39;ctl00$cph_Main$ctl00$toolbox$rptAction$ctl01$lbtAction&#39;,&#39;&#39;)"><i class="icon-question-sign"></i>&nbsp;
                            Trợ giúp</a>
                                </td>

                                <td align="center">
                                    <a id="cph_Main_ctl00_toolbox_rptAction_lbtAction_1" title="Thêm mới" class="toolbar btn btn-info" href="../Product/Create"><i class="icon-plus"></i>&nbsp;
                            Thêm mới</a>
                                </td>

                                <td align="center">
                                    <a id="cph_Main_ctl00_toolbox_rptAction_lbtAction_2" title="Cập nhật giá" class="toolbar btn btn-info" href="javascript:__doPostBack(&#39;ctl00$cph_Main$ctl00$toolbox$rptAction$ctl03$lbtAction&#39;,&#39;&#39;)"><i class="icon-ok"></i>&nbsp;
                            Cập nhật giá</a>
                                </td>

                                <td align="center">
                                    <a onclick="javascript:return ConfirmDelete();" id="cph_Main_ctl00_toolbox_rptAction_lbtAction_3" title="Xóa" class="toolbar btn btn-info" href="javascript:__doPostBack(&#39;ctl00$cph_Main$ctl00$toolbox$rptAction$ctl04$lbtAction&#39;,&#39;&#39;)"><i class="icon-trash"></i>&nbsp;
                            Xóa</a>
                                </td>

                            </tr>
                        </table>

                    </div>
                </div>
            </div>
            <div id="hiddenToolBarScroll" class="scrollBox hidden">
                <h4>
                    <i class="icon-list-alt"></i>&nbsp;Danh sách sản phẩm</h4>
                <div class="FloatMenuBar">

                    <div class="ui-widget-content ui-corner-top ui-corner-bottom">
                        <div id="Div1">

                            <div style="float: right;" class="toolbox-content">

                                <table class="toolbar">
                                    <tr>

                                        <td align="center">
                                            <a onclick="javascript:return PopupHelp(&#39;http://www.bizweb.vn/PopupHelp.aspx?faqcat=25&#39;);" id="cph_Main_ctl00_toolbox2_rptAction_lbtAction_0" title="Trợ giúp" class="toolbar btn btn-info" href="javascript:__doPostBack(&#39;ctl00$cph_Main$ctl00$toolbox2$rptAction$ctl01$lbtAction&#39;,&#39;&#39;)"><i class="icon-question-sign"></i>&nbsp;
                            Trợ giúp</a>
                                        </td>

                                        <td align="center">
                                            <a id="cph_Main_ctl00_toolbox2_rptAction_lbtAction_1" title="Thêm mới" class="toolbar btn btn-info" href="../Product/Create"><i class="icon-plus"></i>&nbsp;
                            Thêm mới</a>
                                        </td>

                                        <td align="center">
                                            <a id="cph_Main_ctl00_toolbox2_rptAction_lbtAction_2" title="Cập nhật giá" class="toolbar btn btn-info" href="javascript:__doPostBack(&#39;ctl00$cph_Main$ctl00$toolbox2$rptAction$ctl03$lbtAction&#39;,&#39;&#39;)"><i class="icon-ok"></i>&nbsp;
                            Cập nhật giá</a>
                                        </td>

                                        <td align="center">
                                            <a onclick="javascript:return ConfirmDelete();" id="cph_Main_ctl00_toolbox2_rptAction_lbtAction_3" title="Xóa" class="toolbar btn btn-info" href="javascript:__doPostBack(&#39;ctl00$cph_Main$ctl00$toolbox2$rptAction$ctl04$lbtAction&#39;,&#39;&#39;)"><i class="icon-trash"></i>&nbsp;
                            Xóa</a>
                                        </td>

                                    </tr>
                                </table>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="widget-body">




            <script type="text/javascript">
                function SelectAllCheckboxes(chk) {
                    $j('#cph_Main_ctl00_ctl00_grvProducts >tbody >tr >td:nth-child(1)').find("input:checkbox").attr('checked', chk.checked);
                }
                //function CheckSelectAtLeastOne() {
                //    var checked = false;
                //    $j('#cph_Main_ctl00_ctl00_grvProducts >tbody >tr >td:nth-child(1)').find("input:checkbox").each(function () {
                //        if (this.checked) { checked = true; }
                //    });
                //    if (checked == false) {
                //        alert('Hãy chọn ít nhất 1 sản phẩm để thao tác');
                //        return false;
                //    } else { return true; }
                //}
                function ConfirmDelete() {
                        var confirmAlert = 'Bạn có chắc chắn muốn xóa các sản phẩm này không?';
                        $j('#cph_Main_ctl00_ctl00_grvProducts >tbody').find("tr").each(function () {
                            var checked = false;
                            var imgChecked = false;
                            $j(this).find('img[src="http://st.hangtot.com/favicon.ico"]').each(function () {
                                imgChecked = true;
                            });
                            $j(this).find("input:checkbox:first").each(function () {
                                if (this.checked) { checked = true; }
                            });
                            if (checked == true && imgChecked == true) {
                                confirmAlert = 'Bạn có chắc chắn muốn xóa sản phẩm này và sản phẩm trên gian hàng hangtot.com?'
                            }
                        });
                        return confirm(confirmAlert);
                    return false;
                }
                function ShowQuickHelp(container, title, desc) {
                    var div = document.createElement("div");
                    div.id = 'help';
                    div.style.zIndex = 1;
                    div.style.display = 'block';
                    div.style.position = 'absolute';
                    div.style.width = '185px';
                    div.style.backgroundColor = '#FEFCD5';
                    div.style.border = 'solid 1px #E7E3BE';
                    div.style.padding = '10px';
                    if (title != '') {
                        div.innerHTML = '<div class="helpTip"><strong>' + title + '</strong></div><br />';
                    }
                    div.innerHTML += '<div style="width:185px; padding-left:0px" class=helpTip>' + desc + '</div>';
                    SetQuickHelpPosition(div, container, 185)
                    container.parentNode.appendChild(div);
                }
                function SetQuickHelpPosition(d, container, width) {
                    var containerX = 0;
                    var containerY = 0;
                    var containerTemp = container;
                    while (containerTemp != null) {
                        containerX += containerTemp.offsetLeft;
                        containerY += containerTemp.offsetTop;
                        containerTemp = containerTemp.offsetParent;
                    }
                    var scrollXY = getScrollXY();
                    var windowRight = document.documentElement.clientWidth;
                    var divX = windowRight - width;
                    var divY = containerY + 15;
                    if (divX <= containerX - scrollXY[0]) {
                        d.style.left = divX + 'px';
                    }
                    else if (width + containerX + 50 > $j(window).width()) {
                        d.style.left = (divX - width - 50) + 'px';
                    }
                    d.style.top = divY + 'px';
                }
                function getScrollXY() {
                    var scrOfX = 0, scrOfY = 0;
                    if (typeof (window.pageYOffset) == 'number') {
                        //Netscape compliant
                        scrOfY = window.pageYOffset;
                        scrOfX = window.pageXOffset;
                    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
                        //DOM compliant
                        scrOfY = document.body.scrollTop;
                        scrOfX = document.body.scrollLeft;
                    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
                        //IE6 standards compliant mode
                        scrOfY = document.documentElement.scrollTop;
                        scrOfX = document.documentElement.scrollLeft;
                    }
                    return [scrOfX, scrOfY];
                }
                function HideQuickHelp(p) {
                    if ($j("#help").length) {
                        $j("#help").css('display', 'none');
                        setTimeout('RemoveHelp()', 1);
                    }
                }
                function RemoveHelp() {
                    $j("#help").remove();
                }
                function ConvertPrice(numberText, culture) {
                    if (numberText == "") return 0;
                    if (culture == "vi-VN") {
                        numberText = numberText.replace(/\./g, "");
                    }
                    else {
                        var s = numberText.length - 2;
                        var c = numberText.substring(s - 1, s);
                        if (c == ".") {
                            numberText = numberText.replace(/\,/g, "");
                        }
                        if (c == ",") {
                            numberText = numberText.replace(/\./g, "");
                            numberText = numberText.replace(/\,/g, ".");
                        }
                    }
                    numberText = numberText.replace(/ /g, '');
                    numberText = encodeURIComponent(numberText);
                    numberText = numberText.replace(/%C2%A0/g, '');
                    numberText = decodeURIComponent(numberText);
                    return Number(numberText);
                }
                function UpdateProduct(linkUpdateId, pId) {
                    var id = $j(linkUpdateId).closest('tr').children(".Id").children("#txtId").val();
                    var code = $j(linkUpdateId).closest('tr').children(".Code").children("#txtCode").val();
                    var price = $j(linkUpdateId).closest('tr').children(".Price").children("#txtPrice").val();
                    var retailPrice = $j(linkUpdateId).closest('tr').children(".RetailPrice").children("#txtRetailPrice").val();
                    var salePrice = $j(linkUpdateId).closest('tr').children(".SalePrice").children("#txtSalePrice").val();
                    var orderNumber = $j(linkUpdateId).closest('tr').children(".OrderNumber").children("#txtOrderNumber").val();
                    var status = $j(linkUpdateId).closest('tr').children(".Status").children("#ddlStatus").val();
                    var isVisible;
                    if ($j('#lbChangeVisible' + pId).children('i').attr('class') == 'icon-eye-open') {
                        isVisible = 'true';
                    }
                    else {
                        isVisible = 'false';
                    }

                    if (price < salePrice) { alert("Giá khuyến mãi phải nhỏ hơn giá bán !"); return false; }

                    var querystring = "{'productid':'" + id + "'," +
                                        "'price':'" + price + "'," +
                                        "'retailprice':'" + retailPrice + "'," +
                                        "'saleprice':'" + salePrice + "'," +
                                        "'ordernumber':'" + orderNumber + "'," +
                                        "'isvisible':'" + isVisible + "'," +
                                        "'code':'" + code + "'," +
                                        "'status':'" + status + "'" + "}";

                    $j.ajax({
                        beforeSend: function () { },
                        type: "POST",
                        url: "/WebServices/ProductService.asmx/UpdateProduct",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        data: querystring,
                        success: function (response) {
                            var result = response.d;
                            if (result == "true") alert("Cập nhật thông tin sản phẩm thành công !");
                            else { if (result != "false") alert(result); }
                        }
                    });

                    return false;

                }
                function UpdateProduct2(pId) {
                    var linkUpdateId = $j("#lbtUpdateProduct" + pId);
                    var id = $j(linkUpdateId).closest('tr').children(".Id").children("#txtId").val();
                    id = pId;
                    var code = $j(linkUpdateId).closest('tr').children(".Code").children("#txtCode").val();
                    var price = $j(linkUpdateId).closest('tr').children(".Price").children("#txtPrice").val();
                    var retailprice = 0;
                    var salePrice = $j(linkUpdateId).closest('tr').children(".SalePrice").children("#txtSalePrice").val();
                    var orderNumber = $j(linkUpdateId).closest('tr').children(".OrderNumber").children("#txtOrderNumber").val();
                    var status = $j(linkUpdateId).closest('tr').children(".Status").children("#ddlStatus").val();
                    var culture = $j(linkUpdateId).closest('tr').children(".Price").children("#txtPrice").attr("culture");
                    var isVisible;
                    if ($j('#lbChangeVisible' + pId).children('i').attr('class') == 'icon-eye-open') {
                        isVisible = 'true';
                    }
                    else {
                        isVisible = 'false';
                    }

                    price = ConvertPrice(price, culture);
                    salePrice = ConvertPrice(salePrice, culture);

                    if (price < salePrice) { alert("Giá khuyến mãi phải nhỏ hơn giá bán !"); return false; }

                    var querystring = "{'productid':'" + id + "'," +
                                        "'price':'" + price + "'," +
                                        "'retailprice' :'" + retailprice + "'," +
                                        "'saleprice':'" + salePrice + "'," +
                                        "'ordernumber':'" + orderNumber + "'," +
                                        "'isvisible':'" + isVisible + "'," +
                                        "'code':'" + code + "'," +
                                        "'status':'" + status + "'}";

                    $j.ajax({
                        beforeSend: function () { },
                        type: "POST",
                        url: "/WebServices/ProductService.asmx/UpdateProduct",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        data: querystring,
                        success: function (response) {
                            var result = response.d;
                            if (result == "true") { }
                            else { alert(result); }
                        }
                    });

                    return false;
                }
                function ChangeVisibleProduct(lnk) {
                    var st = $j(lnk).attr('st');
                    var isvisible = 'true';
                    if (st == 'open') { isvisible = 'false'; }
                    else { isvisible = 'true'; }

                    var productId = $j(lnk).attr('productid');

                    var linkUpdateId = $j("#lbtUpdateProduct" + productId);
                    var id = $j(linkUpdateId).closest('tr').children(".Id").children("#txtId").val();
                    var code = $j(linkUpdateId).closest('tr').children(".Code").children("#txtCode").val();
                    var price = $j(linkUpdateId).closest('tr').children(".Price").children("#txtPrice").val();
                    var retailprice = 0;
                    var salePrice = $j(linkUpdateId).closest('tr').children(".SalePrice").children("#txtSalePrice").val();
                    var orderNumber = $j(linkUpdateId).closest('tr').children(".OrderNumber").children("#txtOrderNumber").val();
                    var status = $j(linkUpdateId).closest('tr').children(".Status").children("#ddlStatus").val();
                    var culture = $j(linkUpdateId).closest('tr').children(".Price").children("#txtPrice").attr("culture");

                    price = ConvertPrice(price, culture);
                    salePrice = ConvertPrice(salePrice, culture);

                    if (price < salePrice) { alert("Giá khuyến mãi phải nhỏ hơn giá bán !"); return false; }

                    //var querystring = "{'productid':'" + productId + "'," +
                    //                    "'price':'" + price + "'," +
                    //                    "'retailprice' :'" + retailprice + "'," +
                    //                    "'saleprice':'" + salePrice + "'," +
                    //                    "'ordernumber':'" + orderNumber + "'," +
                    //                    "'isvisible':'" + isvisible + "'," +
                    //                    "'code':'" + code + "'," +
                    //                    "'status':'" + status + "'}";

                    //$j.ajax({
                    //    beforeSend: function () { },
                    //    type: "POST",
                    //    url: "/WebServices/ProductService.asmx/UpdateProduct",
                    //    contentType: "application/json; charset=utf-8",
                    //    dataType: "json",
                    //    data: querystring,
                    //    success: function (response) {
                    //        var result = response.d;
                    //        if (result == "true") {
                    //            if (st == 'open') {
                    //                $j(lnk).attr('st', 'close');
                    //                $j(lnk).html('<i class="icon-eye-close"></i>');
                    //            }
                    //            else {
                    //                $j(lnk).attr('st', 'open');
                    //                $j(lnk).html('<i class="icon-eye-open"></i>');
                    //            }
                    //        }
                    //        else { console.log(result); }
                    //    }
                    //});

                    return false;
                }
            </script>

            <div class="row-fluid">
                <div class="span3">
                    <div id="sample_1_length" class="dataTables_length">
                        @Html.DropDownList("PageNumber", TsSelectList.GetPageNumber())
                    </div>
                </div>
                <div class="span9 form-search pull-right text-right">
                    <input name="txtSearch" type="text" maxlength="100" id="txtSearch" placeholder="Tên sản phẩm" style="width: 200px;" />
                    @Html.DropDownList("dllProductGroup", TsSelectList.GetProductGroup(), new {style="width: 160px;"})
                    <input type="submit" name="btnSearch" value="Tìm kiếm" id="btnSearch" class="btn" />
                </div>
            </div>
            <div class="row-fluid">
                <div class="span3">
                    <div style="margin-top: 18px;">
                        Tổng số sản phẩm: <span style='color: #A52A2A;'>40</span> / <span style='color: #333;'>100</span>
                    </div>
                </div>
                <div class="span9 pagination pagination-small pagination-right">
                    @Html.PagedListPager(Model, page => Url.Action("Index", new { page = page }), (PagedListRenderOptions)ViewBag.CustomPage)
                </div>
            </div>
            <div>
                <table class="table table-striped table-bordered dataTable table-hover" cellspacing="0" id="cph_Main_ctl00_ctl00_grvProducts" style="border-collapse: collapse;">
                    <tr>
                        <th scope="col">
                            <input id="chkAll" type="checkbox" name="chkAll" onclick="javascript: SelectAllCheckboxes(this);" />
                        </th>
                        <th scope="col">Ảnh</th>
                        <th scope="col">Mã sản phẩm</th>
                        <th scope="col">Tên sản phẩm</th>
                        <th scope="col">Giá</th>
                        <th scope="col">Giá khuyến mại</th>
                        <th scope="col">Trạng thái</th>
                        <th scope="col">Thứ tự</th>
                        <th scope="col">Kho hàng</th>
                        <th scope="col">Thao tác</th>
                    </tr>
                    @foreach (var item in Model)
                    {
                        <tr class="row1">
                            <td align="center" style="width: 8px;">
                                <input id="chkEdit" type="checkbox" name="chkEdit" />
                                <input type="hidden" name="hdEdit" id="hdEdit" value="@item.Id" />
                            </td>
                            <td class="text-center thumb-image" align="center" style="width: 30px;">
                                <img src="http://media.bizwebmedia.net/Thumbnails//Templates/Images/2012/6/16385892312201113305164.30.30.jpg" onerror="this.src='http://media.bizwebmedia.net/Thumbnail.ashx?img=/Templates/Images/2012/6/16385892312201113305164.jpg&width=30&height=30';"
                                product-name="@item.Name"
                                thumbnail="http://media.bizwebmedia.net/Thumbnails//Templates/Images/2012/6/16385892312201113305164.350.300.jpg"
                                thumbnail-error="http://media.bizwebmedia.net/Thumbnail.ashx?img=/Templates/Images/2012/6/16385892312201113305164.jpg&width=350&height=300"
                                alt="@item.Name" />
                            </td>
                            <td class="Code" align="center" style="width: 80px;">
                                <input type="text" id="txtCode" style="width: 70px; text-align: left;" value="@item.Code"
                                onchange="javascript:return UpdateProduct2(@item.Id);" />
                            </td>
                            <td>
                                <a href="../Product/Edit/@item.Id">@item.Name</a>
                            </td>
                            <td class="Price" align="center" style="width: 90px;">
                                <input type="text" id="txtPrice" style="width: 75px; text-align: right;" value="@item.Price"
                                onkeypress="javascript:return keypress(event);"
                                onchange="javascript:return UpdateProduct2(@item.Id);" />
                            </td>
                            <td class="SalePrice" align="center" style="width: 70px;">
                                <input type="text" id="txtSalePrice" style="width: 75px; text-align: right;" value="0"
                                onkeypress="javascript:return keypress(event);"
                                onchange="javascript:return UpdateProduct2(@item.Id);" />
                                <input type="hidden" />
                            </td>
                            <td class="Status" align="center" style="width: 95px;">
                                <select id="ddlStatus" style="width: 95px; text-align: left;" onchange="javascript:return UpdateProduct2(4821018);">
                                    <option value="allow" selected="selected">Đang bán</option>
                                    <option value="disallow">Chỉ hiển thị</option>
                                    <option value="disallowandtext">Hiển thị chữ thay thế</option>
                                </select>
                            </td>
                            <td class="OrderNumber" align="center" style="width: 45px;">
                                <input type="text" id="txtOrderNumber" style="width: 30px;" value="@item.Brand.Name" onchange="javascript:return UpdateProduct2(@item.Id);" />
                            </td>
                            <td style="width: 80px;">
                                <div style="display: none" onmouseout="HideQuickHelp(this)" onmouseover="ShowQuickHelp(this, 'Tình trạng kho hàng', 'Số lượng hiện tại: 0&lt;br /&gt;Cảnh báo khi số lượng còn: 0')"
                                    class="StockLevel">
                                    <span style="width: 0%;" class="InStock"></span>
                                </div>
                            </td>
                            <td class="text-center" align="center" style="width: 100px;">
                                <a href="../Product/Edit/@item.Id" class="action-link-button" id="lbtUpdateProduct"
                                    title="Chỉnh sửa sản phẩm này"><i class="icon-edit"></i></a>
                                @*<a href="/admin.aspx?module=products&act=add&cloneid=4821018&random=4821018&getimage=true" class="action-link-button"
                                    title="Sao chép sản phẩm này"><i class="icon-save"></i></a>*@
                                <a href="#" class="action-link-button" onclick="javascript:(this);"
                                id="lbChangeVisible4821018"
                                productid="@item.Id" st="open"
                                title='Hiển thị'>
                                    <i class='icon-remove'></i>
                                </a>
                            </td>
                        </tr>
                    }
                </table>
            </div>
            <div class="row-fluid">
                <div class="span3">
                </div>
                <div class="span9 pagination pagination-small pagination-right">
                    @Html.PagedListPager(Model, page => Url.Action("Index", new { page = page }), (PagedListRenderOptions)ViewBag.CustomPage)
                </div>
            </div>
            <style type="text/css">
                #tooltip {
                    position: absolute;
                    z-index: 10102;
                    background-color: #FFF;
                    padding: 5px;
                    border: solid 3px #cccccc;
                    display: block;
                    width: 350px;
                    text-align: center;
                }
            </style>
            <script type="text/javascript" src="http://media.bizwebmedia.net/Extensions/Js/jquery.tooltip.min.js"></script>
            <div id="productImagePopup" class="hidden"></div>
            <script type="text/ecmascript">
                $j(function () {
                    $j('.thumb-image img').each(function () {
                        $j(this).bind('mouseover', function () {
                            var respText =
                                    "<img src=\"" + $j(this).attr("thumbnail") + "\" onerror=\"this.src='" + $j(this).attr("thumbnail-error") + "'\" alt=\"" + $j(this).attr("product-name") + "\" />";
                            $j('#productImagePopup').html(respText);
                        });
                        $j(this).tooltip({
                            track: true, delay: 50, showURL: false,
                            bodyHandler: function () {
                                return $j('#productImagePopup').html();
                            }
                        });
                    });
                });
            </script>
        </div>
    </div>
</div>
